package com.wing.oauth2.service;

import cn.hutool.core.collection.CollectionUtil;
import com.wing.common.constant.GlobalConstants;
import com.wing.common.utils.JsonResult;
import com.wing.redis.utils.redis.RedisHelperImpl;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.RoleAuthorityVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 权限-ServiceImpl
 *
 * @author
 */
@Service
@AllArgsConstructor
public class Oauth2ServiceImpl implements Oauth2Service {


    private final RedisHelperImpl redisHelper;
    private final UserClient userClient;

    @Override
    public void refreshAuthorityRoleList() {
        //清空redis缓存
        redisHelper.removeByKeyPrefix(GlobalConstants.URL_AUTH_ROLES_KEY);
        JsonResult<List<RoleAuthorityVO>> result = userClient.authorityRoleList();
        if(JsonResult.hasData(result)){
            List<RoleAuthorityVO> roleAuthorityVOList = result.getData();
            if (CollectionUtil.isNotEmpty(roleAuthorityVOList)) {
                roleAuthorityVOList.stream().forEach(item -> {
                    String authority = item.getAuthority();
                    List<String> roles = item.getRoles();
                    //写入redis缓存
                    redisHelper.valuePut(GlobalConstants.URL_AUTH_ROLES_KEY + authority, roles);
                });
                //redis消息队列
                // redisTemplate.convertAndSend("cleanRoleLocalCache", "true");
            }
        }
    }

}
